jQuery.colorizerating = function()
{
	var good_limit = 300,
		ratings = _j('.post .dd .vote .rating em'),
		len = ratings.length;
	
	function init()
	{
		for (var i = len; i--;)
		{
			colorize(_j(ratings[i]));
		}
	}
	
	function colorize(el)
	{
		var element = el[0],
			rating = parseInt(element.innerHTML);
		
		switch(true)
		{
			case rating > good_limit * 2						: element.style.color = "#0000"+d2h(rating);							break;
			case rating > good_limit && rating<good_limit * 2	: element.style.color = "#00"+d2h(good_limit*2-rating-1)+d2h(rating);	break;
			case rating > 0 && rating <= good_limit				: element.style.color = "#00"+d2h(rating)+"00";							break;
			case rating < 0 && rating >=-good_limit				: element.style.color = "#"+d2h(rating)+"0000";							break;
			case rating <-good_limit							: element.style.color = "#ff0000";										break;
			case rating == 0									: element.style.color = "#e2e2e2";										break;
		}

		if (rating < 0) rating = 0;

		element.style.fontSize = Math.min(16,9+2*Math.log(Math.abs(rating)+1)) + "px"; 
	}
	
	function d2h(d) {d=Math.abs(d); d=d%good_limit; d=good_limit/4+3*d/4; d=Math.round(256*d/good_limit); var str=d.toString(16); if (str.length==2) return str; else return "0"+str;}
	
	if (len) init();
	
	document.addEventListener("DOMNodeInserted", function(e){
		var obj = _j(e.target); if (obj.hasClass('post')) colorize(_j(obj).find('.dd .vote .rating em'));
	}, false);
}

jQuery(document).ready(function(){ 
	jQuery.colorizerating();
});